<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="600"  destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
      <el-form-item label="字典类型" prop="typeId">
        <el-input disabled placeholder="请输入字典名称" v-model="dictType.nameAndType"></el-input>
      </el-form-item>
      <el-form-item label="字典键" prop="k">
        <el-input v-model="form.k" placeholder="请输入字典键" clearable></el-input>
      </el-form-item>
      <el-form-item label="字典值" prop="v">
        <el-input v-model="form.v" placeholder="请输入字典值" clearable></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="form.sort" :min="1" :max="10" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible=false" >取 消</el-button>
      <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>

export default {
  computed: {
    id() {
      return id
    }
  },
  emits: ['success', 'closed'],
  data() {
    return {
      mode:'add',
      titleMap: {
        add: '新增字典键值',
        edit: '编辑字典键值',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      dictType: {},
      //表单数据
      form: {
        id:"",
        typeId: "",
        name: "",
        key: "",
        value: "",
        sort:1,
        remark:""
      },
      //验证规则
      rules: {
        // typeId: [
        //   {required: true, message: '字典类型不能为空！',change:'blur'}
        // ],
        k: [
          {required: true, message: '字典键不能为空！',change:'blur'},
        ],
        v: [
          {required: true, message: '字典值不能为空！',change:'blur'},
        ],
        sort:[
          {required: true,change:'blur',type:'number'},
        ]
      },
    }
  },
  methods: {
    //点击节点
    handleNodeClick(data,checked){
      if(checked){
        this.$refs.tree.setCheckedNodes([data])
      }
    },
    //显示
    open(mode='add'){
      this.mode = mode;
      this.visible = true;
      return this
    },
    //表单提交方法
    submit(){
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          let res = {};
          if(this.mode == 'add'){
            res = await this.$API.system.dictItem.addItem.post(this.form)
          } else {
            res = await this.$API.system.dictItem.updateItem.post(this.form);
          }
          this.isSaveing = false;
          if(res.code == 200){
            this.$emit('success', this.form, this.mode)
            this.visible = false;
            this.$message.success("操作成功")
          }else{
            this.$alert(res.message, "提示", {type: 'error'})
          }
        }
      })
    },
    //表单注入数据
    async setData(id,typeId){
      if(id) {
        let res1 = await this.$API.system.dictItem.detail.get(id)
        this.form =res1.data
      }
      let res2 = await this.$API.system.dict.detail.get(typeId)
      this.dictType = res2.data
      this.form.typeId =typeId
      this.dictType['nameAndType'] = this.dictType.name + "(" + this.dictType.type + ")"
    }
  }
}
</script>

<style scoped>

</style>
